﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using EnvironmentalMonitor.Support.Hibernate.Environmental;
using EnvironmentalMonitor.Support.Hibernate.Manage;
using EnvironmentalMonitor.Support.Hibernate;
using EnvironmentalMonitor.Support.Module.Environmental;
using EnvironmentalMonitor.Support.Module.Manage;
using EnvironmentalMonitor.Support.Module;

namespace EnvironmentalMonitor.Support.Business.Environmental
{
    /// <summary>
    /// 岗位和设备关系业务类
    /// </summary>
    public class PostMachineBusiness
    {
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="rows">每页行数</param>
        /// <param name="total">总数</param>
        /// <returns>岗位和设备关系集合</returns>
        public List<PostMachine> Query(int page, int rows, ref int total)
        {
            List<PostMachine> results = new List<PostMachine>();

            PostMachineHibernate hibernate = new PostMachineHibernate();
            results = hibernate.Query(page, rows, ref total);

            return results;
        }

        /// <summary>
        /// 通过岗位查询
        /// </summary>
        /// <param name="post">岗位</param>
        /// <param name="total">总数</param>
        /// <returns>岗位和设备关系集合</returns>
        public List<PostMachine> QueryByPost(Post post, ref int total)
        {
            List<PostMachine> results = new List<PostMachine>();

            PostHibernate postHibernate = new PostHibernate();
            post = postHibernate.QueryByGuid(post.Guid);

            MachineHibernate machineHibernate = new MachineHibernate();
            List<Machine> machines = machineHibernate.Query(1, int.MaxValue, ref total);

            PostMachineHibernate postMachineHibernate = new PostMachineHibernate();
            List<PostMachine> postMachines = postMachineHibernate.QueryByPost(post.Guid);

            for (int i = 0; i < machines.Count; i++)
            {
                PostMachine postMachine = new PostMachine();
                postMachine.PostId = post.Guid;
                postMachine.MachineId = machines[i].Guid;
                postMachine.Post = post;
                postMachine.Machine = machines[i];

                for (int j = 0; j < postMachines.Count; j++)
                {
                    if (machines[i].Guid == postMachines[j].MachineId)
                    {
                        postMachine = postMachines[j];
                        break;
                    }
                }
                results.Add(postMachine);
            }
            total = results.Count;

            return results;
        }

        /// <summary>
        /// 通过用户查询
        /// </summary>
        /// <param name="user">用户</param>
        /// <returns>岗位和设备关系集合</returns>
        public List<PostMachine> QueryByUser(User user)
        {
            List<PostMachine> results = new List<PostMachine>();

            if (user != null)
            {
                PostMachineHibernate hibernate=new PostMachineHibernate();
                int total = 0;
                if (user.Prerogative)
                {
                    results = hibernate.Query(1, int.MaxValue, ref total);
                }
                else
                {
                    results = hibernate.QueryByUser(user.Guid);
                }
            }

            return results;
        }

        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="value">值</param>
        /// <returns>结果</returns>
        public bool Insert(PostMachine value)
        {
            bool result = false;

            PostMachineHibernate hibernate = new PostMachineHibernate();
            result = hibernate.Insert(value);

            return result;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="guid">主键</param>
        /// <returns>结果</returns>
        public bool Delete(string guid)
        {
            bool result = false;

            PostMachineHibernate hibernate = new PostMachineHibernate();
            result = hibernate.Delete(guid);

            return result;
        }
    }
}
